/*
 * NativeTestCase.java  0.4.0 / Oct 25, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.nunit;

import junit.framework.TestCase;

import com.cute4j.Cute4j;

/**
 * Native Test Case.
 *
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class NativeTestCase extends TestCase {

// Static Initialization. ------------------------------------------------------

  static {
    Cute4j.initialize();
    _initialize();
  }

// Package-Private Field. ------------------------------------------------------

  long _testObject;

// Protected Methods. ----------------------------------------------------------

  /**
   * Fails native test.
   *
   * @param condition the condition
   * @param fileName the file name
   * @param lineNumber the line number
   */
  protected void failCondition(String condition, String fileName,
                               int lineNumber) {
    fail(formatMessage(condition, fileName, lineNumber));
  }

  /**
   * Formats error message.
   *
   * @param condition the condition
   * @param fileName the file name
   * @param lineNumber the line number
   *
   * @return formatted error message.
   */
  protected String formatMessage(String condition, String fileName,
                                 int lineNumber) {
    StringBuffer sb;
    if (condition == null) {
      sb = new StringBuffer("Native condition failed");
    } else {
      sb = new StringBuffer("Native condition \"");
      sb = sb.append(condition);
      sb.append("\" failed");
    }
    if (fileName != null) {
      sb.append("\n in file: ");
      sb.append(fileName);
    }
    if (lineNumber >= 0) {
      sb.append("\n at line: ");
      sb.append(lineNumber);
    }
    return sb.toString();
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected void setUp() throws Exception {
    _testObject = _createTestObject();
    if (_testObject == 0) {
      fail("Could not create test object");
    }
  }

  /**
   * {@inheritDoc}
   */
  protected void tearDown() throws Exception {
    _deleteTestObject(_testObject);
  }

// Protected Native Methods. ---------------------------------------------------

  /**
   * Creates native test object.
   * <p>
   *   <tt>NOTE: You must override this method in the native code!</tt>
   * </p>
   *
   * @return test object.
   */
  protected native long _createTestObject();

  /**
   * Deletes native object.
   *
   * @param testObject the test object
   */
  protected native void _deleteTestObject(long testObject);

// Private Static Method. ------------------------------------------------------

  private static native void _initialize();

}
